package com.zha.boss;


import com.zha.boss.dao.mapper.DicPayTypeMapper;
import com.zha.boss.dao.mapper.DicPayWayMapper;
import com.zha.boss.dao.result.DicItem;
import com.zha.boss.dao.result.PayTypeGroup;
import jakarta.annotation.Resource;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;

import javax.lang.model.SourceVersion;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@EnableAsync
@MapperScan(basePackages = "com.zha.boss.dao.mapper")
@SpringBootApplication
public class PowerBossApplication implements CommandLineRunner {
    @Bean
    public ThreadPoolExecutor poolExecutor(){
        return new ThreadPoolExecutor(34,45,10, TimeUnit.MINUTES,new LinkedBlockingQueue<>());
    }
    public static void main(String[] args) {
        SpringApplication.run(PowerBossApplication.class, args);
        System.out.println("Hello, PowerBoss!" );
    }
    // spring容器对象
    @Resource
    private ApplicationContext applicationContext;
    // 支付方式的字典数据
    public static List<DicItem> DIC_PAYWAY_LIST = new ArrayList<>();
    // 支付类型的字典数据
    public static Map<String,List<DicItem>> DIC_TYPE_MAP = new HashMap<>();
    // 支付方式的名称
    public static Map<String,String> PAY_WAY_NAME_MAP = new HashMap<>();
    // 支付类型的名称
    public static Map<String,String> PAY_TYPE_NAME_MAP = new HashMap<>();
    /**
     * run: 在spring容器初始化完成后执行的
     * @param args
     * @throws Exception
     */
    @Override
    public void run(String... args) throws Exception {
        // 初始化支付方式的字典数据
        DicPayWayMapper payWayMapper = applicationContext.getBean(DicPayWayMapper.class);
        // 自定义Sql语句，初始化字典数据
        DIC_PAYWAY_LIST = payWayMapper.selectPayWayDic();
        // 将List<DicItem>转为Map<String,String>
        PAY_WAY_NAME_MAP = DIC_PAYWAY_LIST.stream().collect(Collectors.toMap(DicItem::getCode, DicItem::getLabel));

        // 初始化支付类型的字典数据
        DicPayTypeMapper dicPayTypeMapper = applicationContext.getBean(DicPayTypeMapper.class);
        /**
         * for (DicItem dicItem : DIC_PAYWAY_LIST) {
         *             String payWayCode = dicItem.getCode();
         *             // 支付类型List
         *             List<DicItem> dicItemList = dicPayTypeMapper.selectPayTypeByWayCode(payWayCode);
         *             DIC_TYPE_MAP.put(payWayCode, dicItemList);
         *             // 使用pay_way_code:pay_type_code作为key,值是pay_type_name
         *             for (DicItem payType : dicItemList) {
         *                 PAY_TYPE_NAME_MAP.put(payWayCode + ":" + payType.getCode(), payType.getLabel());
         *             }
         *         }
         *
         */
        List<PayTypeGroup>  payTypeGroupList = dicPayTypeMapper.selectGroupConcat();
        for (PayTypeGroup payTypeGroup : payTypeGroupList) {
            String wayCode = payTypeGroup.getWayCode();
            // PC_PAY#PC支付;SCAN PAY#扫码支付
            String groupContent = payTypeGroup.getGroupContent();
            String[] groupSplit = groupContent.split(";");
            // PC_PAY#PC支付
            List<DicItem> dicItemList = new ArrayList<>();
            for (String groupData : groupSplit) {
                String typeCode = groupData.split("#")[0];
                String typeName = groupData.split("#")[1];
                dicItemList.add(new DicItem(typeCode,typeName));
                // 支付类型
                PAY_TYPE_NAME_MAP.put(wayCode + ":" + typeCode, typeName);
            }
            DIC_TYPE_MAP.put(wayCode,dicItemList);
        }
    }
}
